if ('addEventListener' in document) {  
    document.addEventListener('DOMContentLoaded', function() {  
        FastClick.attach(document.body);  
    }, false);  
} 
$(function() {
    FastClick.attach(document.body);
});
var provinces = new Array("京","沪","浙","苏","粤","鲁","晋","冀",
            "豫","川","渝","辽","吉","黑","皖","鄂",
            "津","贵","云","桂","琼","青","新","藏",
            "蒙","宁","甘","陕","闽","赣","湘");

var keyNums = new Array("1","2","3","4","5","6","7","8","9","0",
            "Q","W","E","R","T","Y","U","I","O","P",
            "A","S","D","F","G","H","J","K","L","清空",
            "确定","Z","X","C","V","B","N","M","删除");

var carInput = $("#binging_con_top_content_con_input_con_input").val();
const getParam=(param)=>{
	var reg = new RegExp('(^|&)' + param + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
	return unescape(r[2]);
	}
	return null;
};
var exitId=getParam('ex');
var next=0;
/*----全局变量--*/
/*	注册一个load弹框*/
var load;
var isDef=false;//是否默认
	function showProvince(){
			$("#pro").html("");
			var ss="";
			for(var i=0;i<provinces.length;i++){
				ss=ss+addKeyProvince(i)
			} 
			$("#pro").html("<ul class='clearfix ul_pro'>"+ss+"<li class='li_close' onclick='closePro();'><span>关闭</span></li><li class='li_clean' onclick='cleanPro();'><span>清空</span></li></ul>");
	} 
	function showKeybord(){
			$("#pro").html("");
			var sss="";
			for(var i=0;i<keyNums.length;i++){
				sss=sss+'<li data-key="'+i+'" class="ikey ikey'+i+' '+(i>9?"li_zm":"li_num")+' '+(i>29?"li_w":"")+'" ><span onclick="choosekey(this,'+i+');">'+keyNums[i]+'</span></li>'
			} 
			$("#pro").html("<ul class='clearfix ul_keybord'>"+sss+"</ul>");
	}
    function addKeyProvince(provinceIds){
        var addHtml = '<li>';
            addHtml += '<span onclick="chooseProvince(this);">'+provinces[provinceIds]+'</span>';
            addHtml += '</li>';
            return addHtml;
    }

    function chooseProvince(obj){
       $(".input_pro span").text($(obj).text());
	   $(".input_pro").addClass("hasPro");
	   $(".input_pp").find("span").text("");
       $(".ppHas").removeClass("ppHas");
	   next=0;
	   showKeybord();
	};
	function choosekey(obj,jj){
		if(jj==29){
			$('.ul_input span').text("");
			$(".hasPro").removeClass("hasPro");	
			showProvince();
			next=0;			   
			   
		}else if(jj==30){
			var span1 = $("#span1").text();
			var span2 = $("#span2").text();
			var span3 = $("#span3").text();
			var span4 = $("#span4").text();
			var span5 = $("#span5").text();
			var span6 = $("#span6").text();
			var span7 = $("#span7").text();
			var span8 = $("#span8").text();
			var carNumber = span1+span2+span3+span4+span5+span6+span7+span8;
			/** 正则检验车牌*/
			if(!isVehicleNumber(carNumber)){
				layer.msg("请检查车牌号是否正确", {icon: 2,shade: [0.8, '#393D49'],time: 1500});
				return;
			}else{
				load=layer.msg('加载中。。。', {
					  icon: 16
					  ,shade: 0.01
					  ,time: 0
				});							
				 //发送ajax绑定车牌
				if($(".check-item-icon").hasClass('checked')){
					isDef=true;
				};
				 var dataObj = {
					 carNum:carNumber,
					 isDef:isDef
				 }
				 $.ajax({
					 url:"/member/carplate/bind",
					 method:"POST",
					 data:JSON.stringify(dataObj),
					 //async:false,
					 context:this,
					 //dataType:"json", //设置回调数据的格式
					 contentType:"application/json;charset=UTF-8",
					 success:function(result){
						 layer.close(load)//关闭加载窗
						 //请求成功
						 if(result.status == 0){
							  layer.msg('绑定成功', {icon: 1,shade: [0.8, '#393D49'],time: 1000},function(){
								  location.href ="/member/park/payment?ex="+ exitId;
							  });
						 }else{
							  layer.msg(result.err, {icon: 5,shade: [0.8, '#393D49'],time: 1000});						 							 
						 }
					 },
					 error:function(error){
						  layer.close(load)//关闭加载窗
						  layer.msg(result.err, {icon: 5,shade: [0.8, '#393D49'],time: 1000});						  					 
					 }
				  })
			}
			//layer.closeAll();
		}else if(jj==38){
			if($(".ppHas").length==0){
				$(".hasPro").find("span").text("");			
				$(".hasPro").removeClass("hasPro");	
				showProvince();
				next=0;
			}
			$(".ppHas:last").find("span").text("");			
			$(".ppHas:last").removeClass("ppHas");	
			next=next-1;
			if(next<1){
				next=0;
			}
			console.log(next);
		}else{
			if(next>6){
				return
			}
			console.log(next);
			for(var i = 0; i<$(".input_pp").length;i++){
				if(next==0 & jj<10 & $(".input_pp:eq("+next+")").hasClass("input_zim")){
					layer.msg("车牌第二位为字母", {icon:2,shade: [0.8, '#393D49'],time: 1500});				
					return
				}
				$(".input_pp:eq("+next+")").find("span").text($(obj).text());
				$(".input_pp:eq("+next+")").addClass("ppHas");
				next=next+1;
				if(next>5){
					next=6;
				}
				getpai();
				return
			}
			
		}
	}
	function closePro(){
       $('#pro').hide();
	}		
	function cleanPro(){
       $(".ul_input").find("span").text("");
       $(".hasPro").removeClass("hasPro");
       $(".ppHas").removeClass("ppHas");
	   next=0;
	}	
	function trimStr(str){return str.replace(/(^\s*)|(\s*$)/g,"");}
	function getpai(){
		var pai=trimStr($(".car_input").text());
		$(".car_input").attr("data-pai",pai);
	}
	
	  //车牌号验证方法  
	  function isVehicleNumber(vehicleNumber) {
	    var xreg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
	    var creg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
	    if(vehicleNumber.length == 7){
	     return creg.test(vehicleNumber);
	  } else if(vehicleNumber.length == 8){
	     return xreg.test(vehicleNumber);
	   } else{
	     return false;
	   }
	 }
;function initKeybord(){
//	默认显示省份
	showProvince();		
};	

window.onload = function() {
	initKeybord();
	$(".input_pro").click(function(){
		initKeybord();
	});
	$(".input_pp").click(function(){
			$('#pro').show();
		 if($(".input_pro").hasClass("hasPro")){ // 如果已选择省份
			 showKeybord()
		 }else{
			 $(".input_pro").click()
		 }
	})
	

}
//点击tip下面的提交按钮
$('#submit').click(function(){
	choosekey('span','30');
});
/*设置为默认车牌*/
$(".check-item").click(function(){
	$(".check-item-icon").toggleClass('checked')
});


